'use strict'
const _ = require('lodash');
const _const = require('../../commons/const');

/**
 * 存放所有的权限标识码
 *  (可单独为每个接口 定义 最基础的（增删改查权限)
 * 
 * 注：现在为了方便 权限就定义的颗粒度 粗一点
 */
const PERMISSIONS = {
  /**超级权限标识 */
  ROOT: 'admin',
  WAREHOUSE: 'warehouse',
  OPERATION: 'operation'


}
const PERMISSIONS_ARR = _.values(PERMISSIONS) || [];

/**
 * 角色 -- 权限 
 */
const ROLE_PERMISSION_CODE = {
  [_const.ADMIN_USER_ROLE.ROOT_ADMIN_USER]: [PERMISSIONS.ROOT],
  [_const.ADMIN_USER_ROLE.ROOT_WAREHOUSE_USER]: [PERMISSIONS.WAREHOUSE],
  [_const.ADMIN_USER_ROLE.ROOT_OPERATION_USER]: [PERMISSIONS.OPERATION],
}

/**
 * 检查权限 
 * @param {*} myPermision 
 * @param {*} requiredPermissions 
 */
function checkPermision(myPermision = [], requiredPermissions = []) {
  if (myPermision.indexOf(PERMISSIONS.ROOT) >= 0) { // 如果是ROOT权限先直接跳过
    return true;
  }
  return myPermision.some(code => {
    return requiredPermissions.includes(code)
  })
}




module.exports = {
  checkPermision,
  PERMISSIONS,
  PERMISSIONS_ARR,
  ROLE_PERMISSION_CODE
}